// PluginICFnLibSampleApp.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

#include "IMxPluginLibDetails.h"
#include "MxPluginManager.h"

#include "IPluginICFnLibSample.h"
#include "IPluginXmlLib.h"
#include "IPluginICProcSteps.h"

bool InitPlugins(MxPluginManager *plugins);

int main(int argc, char* argv[])
{
	std::cout << "PluginICFnLibSampleApp v1.0.0.2 - program starts" << std::endl << std::endl;
	
	MxPluginManager plugins;
	if (InitPlugins(&plugins) == true)
	{
		MxPluginLib *pluginFnLibSample = '\0';
		if ((pluginFnLibSample = plugins.GetPlugin(IPluginICFnLibSampleLibID::GUID())) == '\0')	
			std::cout << "Error: MxPlugin PluginICFnLibSample LibID=[" << IPluginICFnLibSampleLibID::GUID() << "] not found" << std::endl;
		else
		{
			MxPluginLib *pluginXml = '\0';
			if ((pluginXml = plugins.GetPlugin(IPluginXmlLibID::GUID())) == '\0')	
				std::cout << "Error: MxPlugin PluginXml LibID=[" << IPluginXmlLibID::GUID() << "] not found" << std::endl;
			else
			{
				MxPluginLib *pluginProcSteps = '\0';
				if ((pluginProcSteps = plugins.GetPlugin(IPluginICProcStepsLibID::GUID())) == '\0')	
					std::cout << "Error: MxPlugin PluginICProcSteps LibID=[" << IPluginICProcStepsLibID::GUID() << "] not found" << std::endl;
				else
				{
					IPluginICFnLibSample  *fnlib = '\0';
					if ((fnlib =  static_cast<IPluginICFnLibSample *>(pluginFnLibSample->CreateClassInstance(IPluginICFnLibSampleIID::GUID()))) != '\0')
					{
						std::cout << fnlib->GetLibName() << " v" << fnlib->GetLibVersion() << " - " << fnlib->GetLibCopyrightNotice() << std::endl;
						std::cout << "support: " << fnlib->GetLibSupportSite() << std::endl;
						std::cout << "licence: " << fnlib->GetLibLicenseName() << " (" << fnlib->GetLibLicenseURL() << ")" << std::endl;
						std::cout << "description: " << fnlib->GetLibDescription() << std::endl << std::endl;

						IPluginXmlLib  *xmllib = '\0';
						if ((xmllib =  static_cast<IPluginXmlLib *>(pluginXml->CreateClassInstance(IPluginXmlLibIID::GUID()))) != '\0')
						{
							std::cout << xmllib->GetLibName() << " v" << xmllib->GetLibVersion() << " - " << xmllib->GetLibCopyrightNotice() << std::endl;
							std::cout << "support: " << xmllib->GetLibSupportSite() << std::endl;
							std::cout << "licence: " << xmllib->GetLibLicenseName() << " (" << xmllib->GetLibLicenseURL() << ")" << std::endl;
							std::cout << "description: " << xmllib->GetLibDescription() << std::endl << std::endl;

							IPluginICProcSteps  *procStepslib = '\0';
							if ((procStepslib =  static_cast<IPluginICProcSteps *>(pluginProcSteps->CreateClassInstance(IPluginICProcStepsIID::GUID()))) != '\0')
							{
								std::cout << procStepslib->GetLibName() << " v" << procStepslib->GetLibVersion() << " - " << procStepslib->GetLibCopyrightNotice() << std::endl;
								std::cout << "support: " << procStepslib->GetLibSupportSite() << std::endl;
								std::cout << "licence: " << procStepslib->GetLibLicenseName() << " (" << procStepslib->GetLibLicenseURL() << ")" << std::endl;
								std::cout << "description: " << procStepslib->GetLibDescription() << std::endl << std::endl;

								procStepslib->DestroyPtr();
							}
							xmllib->DestroyPtr();
						}
						fnlib->DestroyPtr();
					}
				}
			}
		}
	}
	std::cout << std::endl << "PluginICFnLibSample - program ends" << std::endl;
	return 0;
}

bool InitPlugins(MxPluginManager *plugins)
{
	bool rc = false;

	DWORD errorCode = MxPluginManager::UnknownError;
	wchar_t  szPath[MAX_PATH];
	if ( GetModuleFileName( NULL, szPath, sizeof(szPath) / sizeof (wchar_t) ) != 0 )
	{
		std::wstring exePathFilename(szPath);
		std::basic_string <char>::size_type endPath = -1;
		static const std::basic_string <char>::size_type npos = -1;
		if ((endPath = exePathFilename.rfind('\\', exePathFilename.length() )) != npos)
		{
			std::wstring appFolder(exePathFilename.substr(0, endPath));
			errorCode = plugins->RefreshPluginsInFolder(appFolder.c_str(), IMxPluginLibDetailsIID::GUID());
		}
	}
	if (errorCode == MxPluginManager::NoError )
		rc = true;
	else
	{
		std::ostringstream out;
		out << "Error: RefreshPlugins returned error code " << errorCode << std::endl << " error codes:" << std::endl << MxPluginManager::ErrorCodeList << std::endl << "For other errors see MSDN System Error Codes";
		std::cout << out.str();
	}
	return rc;
}

